package com.mapeditor.toolbar {
	import AS3Events.AS3EventMessage;
	import AS3Events.AS3EventRouter;
	import AS3Util.Tr;
	import com.mapeditor.math.Vector3;
	import flash.display.GradientType;
	import flash.display.Graphics;
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.HTTPStatusEvent;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.geom.Matrix;
	
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.net.URLRequest;
	
	
	public class SmallButton extends MovieClip{
		
	
		/************************************************************
		 * MovieClip que vai conter o conteudo do botao.
		 ***********************************************************/
		private var _btnImg:MovieClip;
		private var _btnIcon:Loader;
		private var _fgDown:Boolean;
		private var _hint:String;
		private var _hintPos:Boolean;
		private var _p4SmallButton:Boolean;
		private var _content:Sprite;
		private var _fgSelected:Boolean;
		private var _defaultColor:uint;
		private var _disableColor:uint;
		private var _fgBackGround:Boolean;
		private var _height:Number;
		private var _width:Number;
		/************************************************************
		 * CONSTRUTOR PADRAO
		 ***********************************************************/		
		public function SmallButton(w:Number = 16, h:Number=16) {
			_defaultColor = 0x003299;
			_disableColor = 0x666666;
			_fgBackGround = true;
			_height = h;
			_width = w;
			_content = new Sprite;
			_hint = "";
			
			//_content.graphics.lineStyle(1, 0x0000FF);
			_content.graphics.drawRect(0, 0, _width, _height);
			
			addChild(_content);
			_fgDown = false;
			_hintPos = false;
			_content.y = 0;
			_content.x = 0;
			
			this.mouseChildren = false;
			this._fgSelected = false;
			
		}//end of method
		
		/************************************************************
		 * Adiciona um moviClipe ao botao		 
		 ***********************************************************/		
		public function setContent(btnImg:MovieClip):void{
			_btnImg = btnImg;
			
			_content.addChild(_btnImg);
			
			_btnImg.x = _content.width/2 - _btnImg.width/2;
			_btnImg.y = _content.height / 2 - _btnImg.height / 2;
			
			
		}//end of method
		
		public function mouseOut():void {
			if (!this._fgSelected)
			{
				this.graphics.clear();			
				this.graphics.beginFill(0xFFFFFF,0);
				this.graphics.drawRect(0, 0, this._width, (_fgBackGround) ? this._height : this.height);
				this.graphics.endFill();
			}
			
			if(_fgDown)
				mouseUp();			
			
			
		}
		
		/**
		 * Metodo que seta se havera ou nao cor de fundo (background) do botao
		 * @param	fg - boolean
		 */
		public function setBackground(fg:Boolean = true):void {
			this._fgBackGround = fg;
		}
		private function renderBackground(g:Graphics,w:Number,h:Number):void {
			var fType:String = GradientType.LINEAR;
			//Colors of our gradient in the form of an array
			var colors:Array = [ 0xFEE496, 0xF6F6F6];
			var colorLine:Array = [ 0xD8DFFF, 0xCCCCCC];
			//Store the Alpha Values in the form of an array
			var alphas:Array = [ 1, 1 ];
			//Array of color distribution ratios.
			//The value defines percentage of the width where the color is sampled at 100%
			var ratios:Array = [ 0, 255 ];
			//Create a Matrix instance and assign the Gradient Box
			var matr:Matrix = new Matrix();
			matr.createGradientBox(w, h, (Math.PI/180)*(-90), 0, 0 );
			//SpreadMethod will define how the gradient is spread. Note!!! Flash uses CONSTANTS to represent String literals
			var sprMethod:String = SpreadMethod.REFLECT;
			g.lineStyle(1,colorLine[0]);
			g.lineGradientStyle(fType, colorLine, alphas, ratios, null, sprMethod);
			
			g.beginGradientFill( fType, colors, alphas, ratios, matr, sprMethod);				
			g.drawRoundRect(0, 0, w, h,6,6);
			g.endFill();
		}
		
		public function mouseOver():void {
			if(_fgBackGround){
				this.graphics.clear();
				renderBackground(this.graphics, this._width, (_fgBackGround) ? this._height : this.height);
			}
		}
		public function mouseDown():void {
			if (!_fgDown)
			{
				_btnImg.x += 1;
				_btnImg.y += 1;
				_fgDown = true;
			}
			
			
		}
		
		public function mouseUp():void
		{
			if (_fgDown)
			{
				_btnImg.x -= 1;
				_btnImg.y -= 1;
				_fgDown = false;
			}
			
		}
		/************************************************************
		 * Adiciona uma imagem ao botao
		 * @param	pathIcon		caminho da imagem
		 * @param	wIcon			Largura do icone 
		 * @param	hIcon			Altura do icone
		 ***********************************************************/				
		public function setContentImg(pathIcon:String,wIcon:Number=19,hIcon:Number=23):void{
			
			_btnIcon = new Loader ();
			_btnIcon.load(new URLRequest(pathIcon));
			_btnIcon.contentLoaderInfo.addEventListener(Event.COMPLETE, _loaded, false, 0, true);
			_btnIcon.addEventListener(IOErrorEvent.IO_ERROR, onIOError)
			_btnIcon.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError)
			_btnIcon.addEventListener(HTTPStatusEvent.HTTP_STATUS, onHTTPStatus)    
			
		}//end of method
		
		
		//error callbacks
		private function onIOError(evt:IOErrorEvent):void{
			Tr.ace("IOError: " + evt.text);
		}
		private function onHTTPStatus(evt:HTTPStatusEvent):void{
			Tr.ace("HTTPStatus: " + evt.status);
		}
		private function onSecurityError(evt:SecurityErrorEvent):void{
			Tr.ace("SecurityError: " + evt.text);
		}
		
		private function _loaded(e:Event):void {
			
			_btnImg = new MovieClip;
			_btnImg.addChild(_btnIcon.content);
			_content.addChild(_btnImg);
			
			_btnImg.x = _content.width / 2 - _width/ 2;			
			_btnImg.y = _content.height / 2 - _height / 2;
		}
		
		
		public function reloadIcon(path:String):void
		{
			_btnImg.removeChild(_btnIcon);
			this._btnIcon.unload();
			this._btnIcon.load( new URLRequest(path));
			_btnImg.addChild(_btnIcon);			
		}
		
		public function setSelected(fg:Boolean = true):void {
			if (fg == this._fgSelected)
				return;
			this._fgSelected = fg;
			
			this.graphics.clear();
			if (fg && this._fgBackGround)
				this.renderBackground(this.graphics, this._width, (_fgBackGround) ? this._height : this.height);
			else
			{
				this.graphics.beginFill(0xFFFFFF,0);
				this.graphics.drawRect(0, 0, this._width, (_fgBackGround) ? this._height : this.height);
				this.graphics.endFill();
			}
			
		}
		public function getFgSelected():Boolean {
			return _fgSelected;
		}
		
		override public function set height(value:Number):void {
			this._height = value;
		}
	}//end of Class
}//end of package